<template>
  <el-dialog
    :visible.sync="visible"
    title="接单信息录入"
    width="60%"
    :before-close="handleClose"
  >
    <el-form
      :model="orderReceivingForm"
      :rules="orderReceivingFormRules"
      ref="orderReceivingFormRef"
      label-width="130px"
    >
      <el-form-item label="接单时间" prop="take_time">
        <el-date-picker
          v-model="orderReceivingForm.take_time"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="datetime"
          placeholder="选择日期时间"
          style="width: 100%"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="服务人员" prop="service_staff_name">
        <el-input
          v-model="orderReceivingForm.service_staff_name"
          placeholder="请输入"
        ></el-input>
      </el-form-item>
      <el-form-item label="服务人员手机号" prop="service_staff_phone">
        <el-input
          v-model="orderReceivingForm.service_staff_phone"
          placeholder="请输入"
        ></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button class="theme-background-color" @click="saveFormData"
        >仅保存</el-button
      >
      <el-button class="theme-background-color" @click="saveFormDataAndMessage"
        >保存并短信通知</el-button
      >
    </span>
  </el-dialog>
</template>

<script>
export default {
  props: {
    orderReceivingNoticeDialogVisible: {
      type: Boolean,
      default: function () {
        return {}
      }
    },
    orderServiceId: {
      type: Number,
      default: function () {
        return {}
      }
    }
  },
  data() {
    var checkphone = (rule, value, callback) => {
      const phoneReg = /(^1[3|4|5|6|7|8|9]\d{9}$)|(^09\d{8}$)/
      if (value === '') {
        callback(new Error('请输入手机号'))
      } else if (!phoneReg.test(value)) {
        // 引入methods中封装的检查手机格式的方法
        callback(new Error('请输入正确的手机号!'))
      } else {
        callback()
      }
    }
    return {
      visible: false,
      orderReceivingForm: {
        take_time: '',
        service_staff_name: '',
        service_staff_phone: ''
      },
      orderReceivingFormRules: {
        take_time: [{ required: true, message: '请选择', trigger: 'change' }],
        service_staff_name: [
          { required: true, message: '请输入', trigger: 'blur' },
          {
            min: 2,
            max: 20,
            message: '长度在 2 到 20 个字符',
            trigger: 'blur'
          }
        ],
        service_staff_phone: [
          { required: true, message: '请选择', trigger: 'blur' },
          { validator: checkphone, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then((_) => {
          this.$emit('update:orderReceivingNoticeDialogVisible', false)
          done()
        })
        .catch((_) => {})
    },
    closeDialog() {
      this.$emit('update:orderReceivingNoticeDialogVisible', false)
    },
    saveFormData() {
      this.$refs.orderReceivingFormRef.validate(async (valid) => {
        if (!valid) return
        this.orderReceivingForm.id = this.orderServiceId
        this.orderReceivingForm.sms_flag = false
        console.log(this.orderReceivingForm)

        const { data: res } = await this.$http.post(
          'admin/OrderBooking/takeOrder',
          this.orderReceivingForm
        )
        if (res.meta.status !== 200) {
          return this.$message.error(res.meta.msg)
        }
        this.$message.success('接单信息录入成功')
        this.visible = false
        this.$emit('update:orderReceivingNoticeDialogVisible', false)
        this.$parent.getDetailData()
      })
    },
    saveFormDataAndMessage() {
      this.$refs.orderReceivingFormRef.validate(async (valid) => {
        if (!valid) return
        this.orderReceivingForm.id = this.orderServiceId
        this.orderReceivingForm.sms_flag = true
        console.log(this.orderReceivingForm)

        const { data: res } = await this.$http.post(
          'admin/OrderBooking/takeOrder',
          this.orderReceivingForm
        )
        if (res.meta.status !== 200) {
          return this.$message.error(res.meta.msg)
        }
        this.$message.success('接单信息录入成功')
        this.visible = false
        this.$emit('update:orderReceivingNoticeDialogVisible', false)
        this.$parent.getDetailData()
      })
    }
  },
  watch: {
    orderReceivingNoticeDialogVisible() {
      console.log(this.orderServiceId)
      this.visible = this.orderReceivingNoticeDialogVisible
      if (!this.visible) {
        this.$refs.orderReceivingFormRef.resetFields()
      }
    }
  }
}
</script>

<style lang="less" scoped>
.el-form {
  width: 80%;
  margin-left: 15px;
  .el-select {
    width: 100%;
  }
}
/deep/ .el-dialog__footer {
  text-align: center;
}
</style>
